﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;
using edowork.com.Model;

namespace WebInvoicingsys.system.officials
{
    public partial class announcement_add : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        private void Reset()
        {
            txt_Title.Text = "";
            txt_p_date.Text = "";
            txt_e_date.Text = "";

            uc_search_announcement.Reset();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ;
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            alert_msg = "";

            edowork.com.Model.announcementInfo announcementInfo = new announcementInfo();

            announcementInfo.Title = txt_Title.Text.Trim();
            announcementInfo.P_date = txt_p_date.Text.Trim();
            announcementInfo.E_date = txt_e_date.Text.Trim();
            announcementInfo.P_range = uc_search_announcement.getValue();
            announcementInfo.Content = txt_content.Text.Trim();

            if (String.IsNullOrEmpty(announcementInfo.Title))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请填写标题。");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(announcementInfo.P_date))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请填写行文日期。");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(announcementInfo.E_date))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请填写有效日期。");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(announcementInfo.P_range))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择通知范围。");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(announcementInfo.Content))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请填写通知内容。");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            //上传附件1
            if (fp_attach1.HasFile == true)
            {
                announcementInfo.Attach1 = DateTime.Now.ToString("yyMMddhhmmss") + "_" + fp_attach1.FileName;
                announcementInfo.Attach1filename = fp_attach1.FileName;

                //上传文件
                string webfilepath = Application["webfilepath"].ToString();
                fp_attach1.SaveAs(webfilepath + announcementInfo.Attach1);
            }

            //上传附件2
            if (fp_attach2.HasFile == true)
            {
                announcementInfo.Attach2 = DateTime.Now.ToString("yyMMddhhmmss") + "_" + fp_attach2.FileName;
                announcementInfo.Attach2filename = fp_attach2.FileName;

                //上传文件
                string webfilepath = Application["webfilepath"].ToString();
                fp_attach2.SaveAs(webfilepath + announcementInfo.Attach2);
            }

            //保存数据入数据库
            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                //保存入announcement表
                sql = String.Format(announcementInfo.SQL_Insert(), announcementInfo.Title, announcementInfo.P_date, announcementInfo.E_date,
                                    announcementInfo.Content, Session["userid"].ToString(), announcementInfo.P_range, Session["departmentid"].ToString(), DateTime.Now.ToString("yyyy-MM-dd"),
                                    announcementInfo.Attach1, announcementInfo.Attach2, announcementInfo.Attach1filename, announcementInfo.Attach2filename);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }

            Reset();

            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Save);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        //重置
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("/system/officials/announcement.aspx");
        }
    }
}